/**
 *  Author：lili
 */
package org.lili.util.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	/**
	 * 计算两个日期之间相隔天数
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static int getDaysBetween(Date date1, Date date2) {
		Calendar d1 = Calendar.getInstance();
		Calendar d2 = Calendar.getInstance();
		d1.setTime(date1);
		d2.setTime(date2);
		return calDays(d1, d2);
	}

	/**
	 * 计算两个日期之间相隔天数
	 * 
	 * @param dateStr1
	 * @param dateStr2
	 * @param pattern
	 * @return
	 */
	public static int getDaysBetween(String dateStr1, String dateStr2,
			String pattern) {
		Calendar d1 = Calendar.getInstance();
		Calendar d2 = Calendar.getInstance();
		d1.setTime(stringToDate(dateStr1, pattern));
		d2.setTime(stringToDate(dateStr2, pattern));
		return calDays(d1, d2);
	}

	/**
	 * 计算相隔天数
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	private static int calDays(Calendar d1, Calendar d2) {
		if (d1.after(d2)) {
			Calendar swap = d1;
			d1 = d2;
			d2 = swap;
		}
		int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
		int y2 = d2.get(Calendar.YEAR);
		if (d1.get(Calendar.YEAR) != y2) {
			do {
				days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);// 得到当年的实际天数
				d1.add(Calendar.YEAR, 1);
			} while (d1.get(Calendar.YEAR) != y2);
		}
		return days;
	}

	/**
	 * 根据时间字符串生成日期
	 * 
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date stringToDate(String dateStr, String format) {
		SimpleDateFormat f = new SimpleDateFormat(format);
		try {
			return f.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取指定格式的日期字符串
	 * 
	 * @param pattern
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date, String pattern) {
		return new SimpleDateFormat(pattern).format(date);
	}

	/**
	 * 日期加天数得到新的日期
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date addDays(Date date, int days) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, days);
		return c.getTime();
	}

	public static void main(String[] args) {
		Date date = new Date();
		Calendar d1 = Calendar.getInstance();
		d1.setTime(date);
		System.out.println(d1.get(Calendar.DAY_OF_MONTH));
		System.out.println(d1.get(Calendar.DAY_OF_WEEK) - 1);
		// int i = getDaysBetween(new Date(), new Date(2012 - 1900, 6, 5));
		// System.out.println(i);
		// i = getDaysBetween("2012-08-05", "2012-07-05", "yyyy-MM-dd");
		// System.out.println(i);
		// String date = DateUtil.dateToString(addDays(new Date(), -370),
		// "yyyy-MM-dd");
		// System.out.println(date);
	}

	/**
	 * 获取指定日期的当月第一天的日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1);
		return c.getTime();
	}

	/**
	 * 获得当前系统时间，格式可以自定义
	 * 
	 * @param format
	 * @return
	 */
	public static String getCurrentTime(String format) {
		Calendar day = Calendar.getInstance();
		return new SimpleDateFormat(format).format(day.getTime());
	}

}
